package com.flying.invest.invest.service;

import java.util.Date;
import java.util.List;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.annotation.Transaction;
import com.flying.common.util.Codes;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.context.ServiceContext;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.model.QueryResult;
import com.flying.framework.security.Principal;
import com.flying.framework.service.AbstractService;
import com.flying.invest.invest.model.InvestmentAnswers;
import com.flying.invest.survey.model.Question;
import com.flying.invest.survey.service.QuestionService;

@Service(value="InvestmentAnswersService", desc="")
public class InvestmentAnswersService extends AbstractService{
    private QuestionService sqs;
    public void setSurveyQuestionService(QuestionService s) {
    	this.sqs = s;
    }
	@MethodInfo("新增")
	@DaoCreate(entity=InvestmentAnswers.ENTITY_NAME)
	public InvestmentAnswers create(
		
			@Param(value=InvestmentAnswers.INVEST_ID, required=true,	desc="投资ID") Long invest_id,
			@Param(value=InvestmentAnswers.SURVEY_ID, required=true,	desc="调查ID") Long survey_id,
			@Param(value=InvestmentAnswers.QUESTION_ID, required=false,	desc="问题ID") Long question_id,
			@Param(value=InvestmentAnswers.USER_ID, required=true,	desc="所属用户") Long user_id,
			@Param(value=InvestmentAnswers.QUESTION, required=true,	desc="问题") String question,
			@Param(value=InvestmentAnswers.CREATOR, required=false,	desc="创建人") String creator,
			@Param(value=InvestmentAnswers.CREATE_TIME, required=false,	desc="创建时间") Date create_time
		
		) {

		Principal p = ServiceContext.getContext().getPrincipal();
		return ModelFactory.create(InvestmentAnswers.class, 
				InvestmentAnswers.CREATOR, (p == null?null: p.getName()),
				InvestmentAnswers.CREATE_TIME, new Date());
	}
    
	@MethodInfo("新增")
	@Transaction
	public ActionResult createBySurveyId (
		
			@Param(value=InvestmentAnswers.INVEST_ID, required=true,	desc="投资ID") Long invest_id,
			@Param(value=InvestmentAnswers.SURVEY_ID, required=true,	desc="调查ID") Long survey_id,
			@Param(value=InvestmentAnswers.USER_ID, required=true,	desc="所属用户") Long user_id
		
		) throws Exception {
		List<Question> qs = this.sqs.findAll(survey_id, 0, Integer.MAX_VALUE).getRows();
		qs.stream().forEach(x -> {
			this.create(invest_id, survey_id, x.getQuestionId(), user_id, x.getQuestion(), null, null);
		});
		return ModelFactory.create(ActionResult.class, 
				Codes.CODE, Codes.SUCCESS);
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=InvestmentAnswers.ENTITY_NAME)
	public ActionResult update(
		
			@Param(value=InvestmentAnswers.ANSWER_ID, required=true,	desc="ID") Long answer_id,
			@Param(value=InvestmentAnswers.ANSWER, required=false,	desc="答案") String answer,
			@Param(value=InvestmentAnswers.UPDATOR, required=false,	desc="修改人") String updator,
			@Param(value=InvestmentAnswers.UPDATE_TIME, required=false,	desc="修改时间") Date update_time
			
		) throws Exception {

		Principal p = ServiceContext.getContext().getPrincipal();
		return ModelFactory.create(ActionResult.class, 
				InvestmentAnswers.UPDATOR, (p == null?null: p.getName()),
				InvestmentAnswers.UPDATE_TIME, new Date());
	}

	@MethodInfo("删除")
	@DaoRemove(entity=InvestmentAnswers.ENTITY_NAME)
	public ActionResult remove(
		
			@Param(value=InvestmentAnswers.ANSWER_ID, required=true,	desc="ID") Long answer_id
		
		) throws Exception {
		
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=InvestmentAnswers.ENTITY_NAME, pageable=true, modelClass=InvestmentAnswers.class)
	public QueryResult<InvestmentAnswers> findAll(
			@Param(value=InvestmentAnswers.INVEST_ID, required=true,	desc="投资ID") Long invest_id,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) throws Exception {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=InvestmentAnswers.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public InvestmentAnswers findById(
		
			@Param(value=InvestmentAnswers.ANSWER_ID, required=true,	desc="ID") Long answer_id
		) throws Exception {
		return null;
	}

}